<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="姓名">
              <a-input placeholder="请输入姓名" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="证件号码">
              <a-input placeholder="请输入证件号码" v-model="queryParam.idNum"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="监室号">
              <j-select-depart placeholder="请选择监室号" v-model="queryParam.orgCode" />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="性别">
                <j-dict-select-tag placeholder="请选择性别" v-model="queryParam.sex" dictCode="sex" />
              </a-form-item>
            </a-col>
            <a-col :xl="12" :lg="14" :md="16" :sm="24">
              <a-form-item label="入所时间">
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.custodyDate_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.custodyDate_end"></j-date>
              </a-form-item>
            </a-col>
            <!--
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="出所时间">
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.releaseDate_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.releaseDate_end"></j-date>
              </a-form-item>
            </a-col>
            -->
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="人员状态">
                <j-dict-select-tag placeholder="请选择人员状态" v-model="queryParam.status" dictCode="fpms_suspects_status" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="本月订购">
                <j-dict-select-tag placeholder="请选择本月订购" v-model="queryParam.orderSign" dictCode="order_sign" />
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="账户余额">
                <a-input placeholder="请输入最小值" class="query-group-cust" v-model="queryParam.balance_begin"></a-input>
                <span class="query-group-split-cust"></span>
                <a-input placeholder="请输入最大值" class="query-group-cust" v-model="queryParam.balance_end"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="总充值金额">
                <a-input placeholder="请输入最小值" class="query-group-cust"
                  v-model="queryParam.rechargeAmount_begin"></a-input>
                <span class="query-group-split-cust"></span>
                <a-input placeholder="请输入最大值" class="query-group-cust" v-model="queryParam.rechargeAmount_end"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="总消费金额">
                <a-input placeholder="请输入最小值" class="query-group-cust"
                  v-model="queryParam.consumptionAmount_begin"></a-input>
                <span class="query-group-split-cust"></span>
                <a-input placeholder="请输入最大值" class="query-group-cust"
                  v-model="queryParam.consumptionAmount_end"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="download" @click="handleExportXls('人员信息表')">导出</a-button>
<!--      <a-button @click="chongzhi" type="danger">重置订购状态</a-button>-->
      <!-- 高级查询区域
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
            -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchChangeOrderSign0"><a-icon type="undo" />修改订购状态</a-menu-item>
          <a-menu-item key="2" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
          <a-menu-item key="3" @click="xgzt"><a-icon type="update" />修改人员状态</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" bordered rowKey="id" class="j-table-force-nowrap" :scroll="{ x: true }"
        :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="pcaSlot" slot-scope="text">
          <div>{{ getPcaText(text) }}</div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link"><a-icon type="down" />更多</a>
            <a-menu slot="overlay">
              <a-menu-item v-if="record.status === '1'">
                <a @click="recharge(record)" v-has="'suspects:recharge'">充值</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="refund(record)" v-has="'suspects:refund'">退款</a>
              </a-menu-item>
              <a-menu-item v-if="record.status === '1'">
                <a @click="medical(record)" v-has="'suspects:yl'">医疗</a>
              </a-menu-item>
              <a-menu-item v-if="record.status === '1'">
                <a @click="placeOrder(record)" v-has="'suspects:order'">下单</a>
              </a-menu-item>



            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <fpms-suspects-modal ref="modalForm" @ok="modalFormOk" />
    <JeecgsFpmsSksjModal ref="JeecgsFpmsSksjModal" @ok="modalFormOk"></JeecgsFpmsSksjModal>
    <JeecgsFpmsTksjModal ref="JeecgsFpmsTksjModal" @ok="modalFormOk"></JeecgsFpmsTksjModal>
    <JeecgsFpmsYlxfModal ref="JeecgsFpmsYlxfModal" @ok="modalFormOk"></JeecgsFpmsYlxfModal>
    <JeecgsFpmsOrderModal ref="JeecgsFpmsOrderModal" @ok="modalFormOk"></JeecgsFpmsOrderModal>
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, putAction, postAction, deleteAction } from '@/api/manage'
import { reset } from '@/api/api'
import FpmsSuspectsModal from './modules/FpmsSuspectsModal'
import JeecgsFpmsSksjModal from '../finance/modules/FpmsSksjModal'
import JeecgsFpmsTksjModal from '../finance/modules/FpmsTksjModal'
import JeecgsFpmsYlxfModal from '../finance/modules/FpmsYlxfModal'
import JeecgsFpmsOrderModal from '../order/modules/FpmsOrderModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import Area from '@/components/_util/Area'
import '@/assets/less/TableExpand.less'
import { mapMutations, mapState } from 'vuex'
import { mixinDevice } from '@/utils/mixin'
import moment from 'dayjs'

const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'


export default {
  name: "FpmsSuspectsList",
  mixins: [JeecgListMixin],
  components: {
    FpmsSuspectsModal,
    JeecgsFpmsSksjModal,
    JeecgsFpmsTksjModal,
    JeecgsFpmsYlxfModal,
    JeecgsFpmsOrderModal
  },
  data () {
    return {
      description: '人员信息表管理页面',
      currentDeptId: '',
      currentDept: {},
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '姓名',
          align: "center",
          sorter: true,
          dataIndex: 'name'
        },
        {
          title: '性别',
          align: "center",
          sorter: true,
          dataIndex: 'sex_dictText'
        },
        {
          title: '监室号',
          align: "center",
          sorter: true,
          dataIndex: 'orgCode_dictText'
        },
        {
          title: '账户余额',
          align: "center",
          sorter: true,
          dataIndex: 'balance'
        },
        {
          title: '本月订购',
          align: "center",
          sorter: true,
          dataIndex: 'orderSign_dictText'
        },
        {
          title: '入所时间',
          align: "center",
          sorter: true,
          dataIndex: 'custodyDate',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '证件号码',
          align: "center",
          dataIndex: 'idNum'
        },
        {
          title: '现住址详址',
          align: "center",
          dataIndex: 'address'
        },
        {
          title: '人员状态',
          align: "center",
          sorter: true,
          dataIndex: 'status_dictText'
        },
        {
          title: '总充值金额',
          align: "center",
          sorter: true,
          dataIndex: 'rechargeAmount'
        },
        {
          title: '总消费金额',
          align: "center",
          sorter: true,
          dataIndex: 'consumptionAmount'
        },
        {
          title: '更新日期',
          align: "center",
          sorter: true,
          dataIndex: 'updateTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 120,
          scopedSlots: { customRender: 'action' },
        }
      ],
      url: {
        // list: "/suspects/fpmsSuspects/list",
        list: "/suspects/fpmsSuspects/departSuspectsList",
        delete: "/suspects/fpmsSuspects/delete",
        deleteBatch: "/suspects/fpmsSuspects/deleteBatch",
        batchChangeOrderSign0: "/suspects/fpmsSuspects/batchChangeOrderSign0",
        xgzt: "/suspects/fpmsSuspects/zgzt",
        exportXlsUrl: "/suspects/fpmsSuspects/exportXls",
        importExcelUrl: "suspects/fpmsSuspects/importExcel",
        rule: {
          num: ruleBaseURL + 'shop_order_num',
          orderNum: ruleBaseURL + 'order_num'
        },

      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created () {
    this.pcaData = new Area()
    this.getSuperFieldList();
  },
  computed: {
    ...mapState('user', ['zhou', 'token']),
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  methods: {
    // 重置
    chongzhi () {
      this.$confirm({
        title: '重置',
        content: '是否确定重置所有人的订购状态',
        onOk () {
          return new Promise(async (resolve, reject) => {
            const res = await reset()
            console.log(res);
            alert('重置订购状态成功')

            setTimeout(Math.random() > 0.5 ? resolve : reject, 100);

          }).catch(() => console.log('Oops errors!'));
        },
        onCancel () { },
      });

    },
    getPcaText (code) {
      return this.pcaData.getText(code);
    },
    initDictConfig () {
    },
    getSuperFieldList () {
      let fieldList = [];
      fieldList.push({ type: 'datetime', value: 'updateTime', text: '更新日期' })
      fieldList.push({ type: 'string', value: 'name', text: '姓名', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sex', text: '性别', dictCode: 'sex' })
      fieldList.push({ type: 'sel_depart', value: 'orgCode', text: '监室号' })
      fieldList.push({ type: 'string', value: 'idNum', text: '证件号码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'address', text: '现住址详址', dictCode: '' })
      fieldList.push({ type: 'date', value: 'custodyDate', text: '入所时间' })
      fieldList.push({ type: 'date', value: 'releaseDate', text: '出所时间' })
      fieldList.push({ type: 'string', value: 'status', text: '人员状态', dictCode: 'fpms_suspects_status' })
      fieldList.push({ type: 'string', value: 'orderSign', text: '本月订购', dictCode: 'order_sign' })
      fieldList.push({ type: 'BigDecimal', value: 'balance', text: '账户余额', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'rechargeAmount', text: '总充值金额', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'consumptionAmount', text: '总消费金额', dictCode: '' })
      fieldList.push({ type: 'string', value: 'remarks', text: '备注', dictCode: '' })
      this.superFieldList = fieldList
    },
    //sz 2021-4-23
    searchReset () {
      this.queryParam = {}
      this.loadData(1);
    },
    loadData (arg) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      //if (this.currentDeptId === '') return;
      let params = this.getQueryParams();//查询条件
      params.depId = this.currentDeptId;
      getAction(this.url.list, params).then((res) => {
        if (res.success && res.result) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
      })
    },
    open (record) {
      //console.log(record);
      this.currentDeptId = record.id;
      this.currentDept = record;
      this.loadData(1);
    },
    clearList () {
      this.currentDeptId = '';
      this.dataSource = [];
    },
    hasSelectDept () {
      if (this.currentDeptId == '') {
        this.$message.error("请选择一个部门!")
        return false;
      }
      return true;
    },
    handleEdit: function (record) {
      console.log(this.$store.state.user.zhou);
      console.log('周开心');
      this.$store.commit('xiugai', true)
      this.$refs.modalForm.title = "编辑";
      this.$refs.modalForm.departDisabled = true;
      this.$refs.modalForm.disableSubmit = false;
      this.$refs.modalForm.edit(record);
    },
    handleAdd: function () {
      this.$store.commit('xiugai', false)
      this.$refs.modalForm.departDisabled = true;
      //初始化负责部门
      this.$refs.modalForm.nextDepartOptions = [{ value: this.currentDept.key, label: this.currentDept.title }]
      this.$refs.modalForm.title = "新增";
      this.$refs.modalForm.edit({ activitiSync: '1', userIdentity: 1, selecteddeparts: this.currentDeptId })
    },

    recharge (record) {
      let suspectId = record.id
      let parameter = { 'id': suspectId };
      let suspect;
      getAction("suspects/fpmsSuspects/queryById", parameter).then(response => {
        if (response.success) {
          suspect = response.result;
          let model = JSON.parse(JSON.stringify(suspect));
          delete model.id;
          delete record.createBy;
          delete record.createTime;

          this.$refs.JeecgsFpmsSksjModal.edit(model);
          this.$refs.JeecgsFpmsSksjModal.title = "充值";
          this.$refs.JeecgsFpmsSksjModal.disableSubmit = false;
        } else {
          this.$message.warning(response.message);
        }
      });
    },

    refund (record) {
      let suspectId = record.id
      let parameter = { 'id': suspectId };
      let suspect;
      getAction("suspects/fpmsSuspects/queryById", parameter).then(response => {
        if (response.success) {
          suspect = response.result;
          let model = JSON.parse(JSON.stringify(suspect));
          delete model.id;
          delete record.createBy;
          delete record.createTime;

          this.$refs.JeecgsFpmsTksjModal.edit(model);
          this.$refs.JeecgsFpmsTksjModal.title = "退款";
          this.$refs.JeecgsFpmsTksjModal.disableSubmit = false;
        } else {
          this.$message.warning(response.message);
        }
      });
    },

    medical (record) {
      let suspectId = record.id
      let parameter = { 'id': suspectId };
      let suspect;
      getAction("suspects/fpmsSuspects/queryById", parameter).then(response => {
        if (response.success) {
          suspect = response.result;
          let model = JSON.parse(JSON.stringify(suspect));
          delete model.id;
          delete record.createBy;
          delete record.createTime;

          this.$refs.JeecgsFpmsYlxfModal.edit(model);
          this.$refs.JeecgsFpmsYlxfModal.title = "其他消费";
          this.$refs.JeecgsFpmsYlxfModal.disableSubmit = false;
        } else {
          this.$message.warning(response.message);
        }
      });
    },

    placeOrder (record) {

      let orderModel = JSON.parse(JSON.stringify(record));

      orderModel.suspectsId = orderModel.id;
      orderModel.suspectsName = orderModel.name;
      orderModel.suspectsBalance = orderModel.balance;
      // this.getNum(record);
      orderModel.orderMonth = moment(new Date()).format('YYYYMM');
      orderModel.orderType = "2";
      orderModel.goodsTotal = 0;
      orderModel.otherFee = 0;
      orderModel.discountFee = 0;
      orderModel.foodFee = 0;
      orderModel.actualPayment = 0;

      delete orderModel.id;
      delete orderModel.createBy;
      delete orderModel.createTime;

      this.$refs.JeecgsFpmsOrderModal.edit(orderModel);
      this.$refs.JeecgsFpmsOrderModal.title = "下单";
      this.$refs.JeecgsFpmsOrderModal.disableSubmit = false;
    },

    batchChangeOrderSign0 () {
      if (!this.url.batchChangeOrderSign0) {
        this.$message.error('请设置url.batchChangeOrderSign0!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = this.selectedRowKeys[0]
        for (var a = 1; a < this.selectedRowKeys.length; a++) {
          ids += "," + this.selectedRowKeys[1]
        }
        var that = this
        this.$confirm({
          title: '确认修改',
          content: '是否将选中人员订购状态改为未订购?',
          onOk: function () {
            deleteAction(that.url.batchChangeOrderSign0, { suspectId: ids }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData2()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    },

    //修改人员状态
    xgzt () {
      if (!this.url.xgzt) {
        this.$message.error('请设置url.xgzt!')
        return
      }
      if (this.selectedRowKeys.status <= 1) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = this.selectedRowKeys[0]
        for (var a = 1; a < this.selectedRowKeys.status; a++) {
          ids += "," + this.selectedRowKeys[1]
        }
        var that = this
        this.$confirm({
          title: '确认修改',
          content: '是否将选中人员状态改为出所?',
          onOk: function () {
            deleteAction(that.url.xgzt, { suspectId: ids }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData2()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    }


  },



}
</script>
<style scoped>
@import '~@assets/less/common.less';
/* @import '../../../../src/assets/less/common.less'; */
</style>